﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Reflection;

namespace SqlFileLayoutViewer {
    public partial class ChooseDatabase : Form {
        public string SQLServerName {
            get {
                return ServerComboBox.Text.Trim();
            }
        }

        public string DatabaseName {
            get {
                return DatabaseComboBox.Text.Trim();
            }
        }

        public string UserID {

            get {
                return userIDTextbox.Text.Trim();
            }

        }

        public string Pwd {
            get {
                return pwdTextbox.Text;
            }
        }

        public ChooseDatabase() {
            InitializeComponent();
        }

        private void ChooseDatabase_Load(object sender, EventArgs e) {
            try {
                // See if we can get a list of local instances 
                List<string> instances = SqlInstanceDiscovery.DiscoverInstances();
                instances.Sort();
                ServerComboBox.Items.AddRange(instances.ToArray());
            }
            catch {
                // Attempt to get a list of instances failed, moving on
            }
        }

        private void DatabaseComboBox_Enter(object sender, EventArgs e) {
            if (this.SQLServerName != "") {

                // Weak attempt to list the databases on the selected SQL instance

                DatabaseComboBox.Items.Clear();

                SqlConnectionStringBuilder cs = new SqlConnectionStringBuilder();
                cs.DataSource = this.SQLServerName;
                cs.InitialCatalog = "master";
                cs.IntegratedSecurity = true;
                cs.ConnectTimeout = 1;

                SqlConnection con = new SqlConnection(cs.ToString());

                try {                    
                    con.Open();
                    DataTable dbtable = con.GetSchema("Databases");
                    con.Close();
                    foreach (DataRow r in dbtable.Rows) {
                        DatabaseComboBox.Items.Add(r["database_name"]);
                    }
                }
                catch {
                    // Failed to list DBs. Move on, nothing to see here.
                }
            }
        }

        private void ComboBoxesChange(object sender, EventArgs e) {
            EnableAnalyzeBtn();
        }

        public void EnableAnalyzeBtn() {
            AnalyzeBtn.Enabled = (this.SQLServerName != "" && this.DatabaseName != "");
        }

        private void SQLAuthCheckbox_CheckedChanged(object sender, EventArgs e) {
            userIDLabel.Enabled = sqlAuthCheckBox.Checked;
            userIDTextbox.Enabled = sqlAuthCheckBox.Checked;
            pwdLabel.Enabled = sqlAuthCheckBox.Checked;
            pwdTextbox.Enabled = sqlAuthCheckBox.Checked;
        }
    }
}
